//
//  FKAddressVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/1/28.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKAddressVC.h"
#import "FKAddressCell.h"
#import "FKAddAddressVC.h"

@interface FKAddressVC ()<UITableViewDataSource, UITableViewDelegate, FKAddressCellDelegate>

@property (nonatomic, weak) UIButton *addBtn;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;
//@property (nonatomic, strong) NSMutableDictionary *param;
@property(nonatomic, assign) NSInteger page;

@property (nonatomic, strong) UIView *emptyView;

@end

@implementation FKAddressVC


- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

//- (NSMutableDictionary *)param
//{
//    if (!_param) {
//        _param = [NSMutableDictionary dictionary];
//        _param[@"type"] = @"1";
//    }
//    return _param;
//}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"收货地址";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UIButton *addBtn = [UIButton fk_btnWithTarget:self action:@selector(addBtnClick) font:fk_adjustFont(16) normalTitle:@"添加收货地址" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
    [self.view addSubview:addBtn];
    self.addBtn = addBtn;
    CGFloat addBtnH = fk_adjustW(49);
    CGFloat addBtnY = fkScreenH - fkStatusAndNaBarH - addBtnH;
    addBtn.frame = CGRectMake(0, addBtnY, fkScreenW, addBtnH);
    addBtn.hidden = true;
    
    CGFloat tableH = addBtnY;
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, tableH) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [tableView.mj_header beginRefreshing];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"refreshAddressList" object:nil];
}

- (void)addBtnClick
{
    FKAddAddressVC *vc = [UIStoryboard storyboardWithName:@"FKAddAddressVC" bundle:nil].instantiateInitialViewController;
    fkPushNav(vc);
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKAddressCell *cell = [FKAddressCell cellWith:tableView];
    cell.data = self.datas[indexPath.section];
    cell.delegate = self;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(140);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return fk_adjustW(10);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

#pragma mark - FKAddressCellDelegate
- (void)addressCell:(FKAddressCell *)cell setDefaultBtnClicked:(NSString *)addressId
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"address_id"] = addressId;
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=set_address" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"设置成功"];
            [self loadData];
        }
    } failure:nil];
}

- (void)addressCell:(FKAddressCell *)cell editBtnClicked:(NSDictionary *)addressData
{
    FKAddAddressVC *vc = [UIStoryboard storyboardWithName:@"FKAddAddressVC" bundle:nil].instantiateInitialViewController;
    vc.addressDict = addressData;
    fkPushNav(vc);
}

- (void)addressCell:(FKAddressCell *)cell deleteBtnClicked:(NSString *)addressId
{
    fkWeakSelf(self);
    [self fk_alertWithTitle:@"确定要删除该地址吗？" confirm:^{
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"address_id"] = addressId;
        [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=del_address" param:param success:^(FKBaseResult *result) {
            if (result.isSuccess) {
                [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                [weakself loadData];
            }
        } failure:nil];
    }];
}

- (UIView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, 200)];
//        _emptyView.backgroundColor = [UIColor redColor];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"address_icon_no"]];
        [_emptyView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
            make.width.mas_equalTo(fk_adjustW(imageView.image.size.width));
            make.height.mas_equalTo(fk_adjustW(imageView.image.size.height));
        }];
        
        UILabel *label = [[UILabel alloc] init];
        [_emptyView addSubview:label];
        label.font = fk_adjustFont(16);
        label.text = @"还没有收货地址，快去添加吧~";
        label.textColor = fkColor999999;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView.mas_bottom).offset(20);
            make.centerX.offset(0);
        }];
        
        UIButton *addBtn = [UIButton fk_btnWithTarget:self action:@selector(addBtnClick) font:fk_adjustFont(15) normalTitle:@"添加地址" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
        [_emptyView addSubview:addBtn];
        [addBtn fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(fk_adjustW(225));
            make.height.mas_equalTo(fk_adjustW(40));
            make.top.equalTo(label.mas_bottom).offset(30);
            make.centerX.offset(0);
        }];
    }
    return _emptyView;
}

#pragma mark - 网络请求
- (void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"page"] = @"1";
    param[@"pagesize"] = @"100";
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=address_list" param:param success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            self.datas = result.data[@"list"];
            [self.tableView reloadData];
            if (self.datas.count > 0) {
                self.addBtn.hidden = false;
            }
        }
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    }];
}



@end
